package com.example.room.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.room.R;
import com.example.room.entity.Student;

import java.util.List;

public class StudentRecycleViewAdapter extends RecyclerView.Adapter<StudentRecycleViewAdapter.MyViewHolder> {

    private List<Student> list;

    public List<Student> getList() {
        return list;
    }

    public void setList(List<Student> list) {
        this.list = list;
    }

    public StudentRecycleViewAdapter(List<Student> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_info, parent, false);
        MyViewHolder holder = new MyViewHolder(itemview);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        TextView tv_id = holder.itemView.findViewById(R.id.tv_id);
        TextView tv_name = holder.itemView.findViewById(R.id.tv_name);
        TextView tv_age = holder.itemView.findViewById(R.id.tv_age);
        tv_id.setText(String.valueOf(list.get(position).id));
        tv_name.setText(list.get(position).name);
        tv_age.setText(String.valueOf(list.get(position).age));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private View itemView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
        }
    }
}
